.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Portions Copyright 1989 AT&T
.\" Copyright 1994, The X/Open Company Ltd.  All Rights Reserved.
.\" Portions Copyright (c) 1998, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH CONNECT 3XNET "Jun 10, 2002"
.SH NAME
connect \- connect a socket
.SH SYNOPSIS
.LP
.nf
\fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lxnet\fR [ \fIlibrary\fR ... ]
#include <sys/socket.h>

\fBint\fR \fBconnect\fR(\fBint\fR \fIsocket\fR, \fBconst struct sockaddr *\fR\fIaddress\fR,
    \fBsocklen_t\fR \fIaddress_len\fR);
.fi

.SH DESCRIPTION
.LP
The \fBconnect()\fR function requests a connection to be made on a socket.  The
function takes the following arguments:
.sp
.ne 2
.na
\fB\fIsocket\fR\fR
.ad
.RS 15n
Specifies the file descriptor associated with the socket.
.RE

.sp
.ne 2
.na
\fB\fIaddress\fR\fR
.ad
.RS 15n
Points to a \fBsockaddr\fR structure containing the peer address.  The length
and format of the address depend on the address family of the socket.
.RE

.sp
.ne 2
.na
\fB\fIaddress_len\fR\fR
.ad
.RS 15n
Specifies the length of the \fBsockaddr\fR structure pointed to by the
\fIaddress\fR argument.
.RE

.sp
.LP
If the socket has not already been bound to a local address,  \fBconnect()\fR
will bind it to an address which, unless the socket's address family  is
AF_UNIX, is an unused local address.
.sp
.LP
If the initiating socket is not connection-mode, then \fBconnect()\fR sets the
socket's peer address, but no connection is made.  For SOCK_DGRAM sockets, the
peer address identifies where all datagrams are sent on subsequent
\fBsend\fR(3XNET) calls, and limits the remote sender for subsequent
\fBrecv\fR(3XNET) calls. If \fIaddress\fR is a null address for the protocol,
the socket's peer address will be reset.
.sp
.LP
If the initiating socket is connection-mode, then \fBconnect()\fR attempts to
establish a connection to the address specified by the \fIaddress\fR argument.
.sp
.LP
If the connection cannot be established immediately and O_NONBLOCK is not set
for the file descriptor for the socket, \fBconnect()\fR will block for up to an
unspecified timeout interval until the connection is established.  If the
timeout interval expires before the connection is established, \fBconnect()\fR
will fail and the connection attempt will be aborted.  If \fBconnect()\fR is
interrupted by a signal that is caught while blocked waiting to establish a
connection, \fBconnect()\fR will fail and set \fBerrno\fR to \fBEINTR\fR, but
the connection request will not be aborted, and the connection will be
established asynchronously.
.sp
.LP
If the connection cannot be established immediately and O_NONBLOCK is set for
the file descriptor for the socket, \fBconnect()\fR will fail and set
\fBerrno\fR to \fBEINPROGRESS\fR, but the connection request will not be
aborted, and the connection will be established asynchronously. Subsequent
calls to \fBconnect()\fR for the same socket, before the connection is
established, will fail and set \fBerrno\fR to \fBEALREADY\fR.
.sp
.LP
When the connection has been established asynchronously, \fBselect\fR(3C) and
\fBpoll\fR(2) will indicate that the file descriptor for the socket is ready
for writing.
.sp
.LP
The socket in use may require the process to have appropriate privileges to use
the \fBconnect()\fR function.
.SH USAGE
.LP
If \fBconnect()\fR fails, the state of the socket is unspecified.  Portable
applications should close the file descriptor and create a new socket before
attempting to reconnect.
.SH RETURN VALUES
.LP
Upon successful completion, \fBconnect()\fR returns 0.  Otherwise, \(mi1 is
returned and \fBerrno\fR is set to indicate the error.
.SH ERRORS
.LP
The \fBconnect()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEADDRNOTAVAIL\fR\fR
.ad
.RS 17n
The specified address is not available from the local machine.
.RE

.sp
.ne 2
.na
\fB\fBEAFNOSUPPORT\fR\fR
.ad
.RS 17n
The specified address is not a valid address for the address family of the
specified socket.
.RE

.sp
.ne 2
.na
\fB\fBEALREADY\fR\fR
.ad
.RS 17n
A connection request is already in progress for the specified socket.
.RE

.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 17n
The \fIsocket\fR argument is not a valid file descriptor.
.RE

.sp
.ne 2
.na
\fB\fBECONNREFUSED\fR\fR
.ad
.RS 17n
The target address was not listening for connections or refused the connection
request.
.RE

.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 17n
The address parameter can not be accessed.
.RE

.sp
.ne 2
.na
\fB\fBEINPROGRESS\fR\fR
.ad
.RS 17n
O_NONBLOCK is set for the file descriptor for the socket and the connection
cannot be immediately established; the connection will be established
asynchronously.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 17n
The attempt to establish a connection was interrupted by delivery of a signal
that was caught; the connection will be established asynchronously.
.RE

.sp
.ne 2
.na
\fB\fBEISCONN\fR\fR
.ad
.RS 17n
The specified socket is connection-mode and is already connected.
.RE

.sp
.ne 2
.na
\fB\fBENETUNREACH\fR\fR
.ad
.RS 17n
No route to the network is present.
.RE

.sp
.ne 2
.na
\fB\fBENOTSOCK\fR\fR
.ad
.RS 17n
The \fIsocket\fR argument does not refer to a socket.
.RE

.sp
.ne 2
.na
\fB\fBEPROTOTYPE\fR\fR
.ad
.RS 17n
The specified address has a different type than the socket bound to the
specified peer address.
.RE

.sp
.ne 2
.na
\fB\fBETIMEDOUT\fR\fR
.ad
.RS 17n
The attempt to connect timed out before a connection was made.
.RE

.sp
.LP
If the address family of the socket is AF_UNIX, then \fBconnect()\fR will fail
if:
.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 16n
An I/O error occurred while reading from or writing to the file system.
.RE

.sp
.ne 2
.na
\fB\fBELOOP\fR\fR
.ad
.RS 16n
Too many symbolic links were encountered in translating the pathname in
\fIaddress\fR.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
A component of a pathname exceeded \fINAME_MAX\fR characters, or an entire
pathname exceeded \fIPATH_MAX\fR characters.
.RE

.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 16n
A component of the pathname does not name an existing file or the pathname is
an empty string.
.RE

.sp
.ne 2
.na
\fB\fBENOTDIR\fR\fR
.ad
.RS 16n
A component of the path prefix of the pathname in \fIaddress\fR is not a
directory.
.RE

.sp
.LP
The \fBconnect()\fR function may fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 16n
Search permission is denied for a component of the path prefix; or write access
to the named socket is denied.
.RE

.sp
.ne 2
.na
\fB\fBEADDRINUSE\fR\fR
.ad
.RS 16n
Attempt to establish a connection that uses addresses that are already in use.
.RE

.sp
.ne 2
.na
\fB\fBECONNRESET\fR\fR
.ad
.RS 16n
Remote host reset the connection request.
.RE

.sp
.ne 2
.na
\fB\fBEHOSTUNREACH\fR\fR
.ad
.RS 16n
The destination host cannot be reached (probably because the host is down or a
remote router cannot reach it).
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The \fIaddress_len\fR argument is not a valid length for the address family; or
invalid address family in sockaddr structure.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
Pathname resolution of a symbolic link produced an intermediate result whose
length exceeds \fIPATH_MAX\fR.
.RE

.sp
.ne 2
.na
\fB\fBENETDOWN\fR\fR
.ad
.RS 16n
The local interface used to reach the destination is down.
.RE

.sp
.ne 2
.na
\fB\fBENOBUFS\fR\fR
.ad
.RS 16n
No buffer space is available.
.RE

.sp
.ne 2
.na
\fB\fBENOSR\fR\fR
.ad
.RS 16n
There were insufficient STREAMS resources available to complete the operation.
.RE

.sp
.ne 2
.na
\fB\fBEOPNOTSUPP\fR\fR
.ad
.RS 16n
The socket is listening and can not be connected.
.RE

.SH ATTRIBUTES
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
.LP
.BR close (2),
.BR poll (2),
.BR select (3C),
.BR sockaddr (3SOCKET),
.BR accept (3XNET),
.BR bind (3XNET),
.BR getsockname (3XNET),
.BR send (3XNET),
.BR shutdown (3XNET),
.BR socket (3XNET),
.BR attributes (7),
.BR standards (7)
